from utils import *
import matplotlib.pyplot as plt

WIDTH = 960
HEIGHT = 540

scene = read_json('sample.json')
cam = scene['camera']
tab = scene['table']
objs = list(scene['objects'].values())
tablez = tab['tablemax'][2]
tablex1 = tab['tablemin'][0]
tablex2 = tab['tablemax'][0]
tabley1 = tab['tablemin'][1]
tabley2 = tab['tablemax'][1]

obj0 = objs[0]
mask = mask_decoder(obj0['mask'])

main_cate, cate_num = get_main_cate(objs)

# plt.matshow(mask, cmap='Greys')
# plt.title("OBJECT0 MASK")
# plt.axis('off')
# plt.show()

# print('center of object0:', get_mask_center(mask))

count = 0

for obj in objs:
    obj['loc'] = [(obj['bboxmax'][0] + obj['bboxmin'][0]) / 2, 
                  (obj['bboxmax'][1] + obj['bboxmin'][1]) / 2,
                  (obj['bboxmax'][2] + obj['bboxmin'][2]) / 2]
    # check visible
    center_in_image = world_to_camera_pixel(obj['loc'], cam)
    obj['visible'] = True if center_in_image != None else False
    obj['topvisible'] = True if ??? else False
    if obj['visible']:
        count += 1

if count < 2:
    exit(0)
